//
//  FFCircle.m
//  ForceField
//
//  Created by Roxanne Brittain on 11/22/11.
//  Copyright (c) 2011 Digifit. All rights reserved.
//

#import "FFCircle.h"

@implementation FFCircle
@synthesize forceVector, viewDelegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    
    CGContextRef c = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(c, 0, 0, 255, 1);
    CGContextSetRGBStrokeColor(c, 0, 0, 0, 1);
    
    CGRect circleRect = CGRectMake(1, 1, self.bounds.size.width-2, self.bounds.size.height-2);
    CGContextFillEllipseInRect(c, circleRect);
    CGContextStrokeEllipseInRect(c, circleRect);
    
    if (forceVector) {
        CGContextSetRGBStrokeColor(c, 0,0,0, 1.0);
        CGPoint center = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
        CGPoint points[2] = {center, CGPointMake(center.x+forceVector->getX(), center.y+forceVector->getY())};
        CGContextStrokeLineSegments(c, points, 2);
    }   
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if ([viewDelegate viewShouldDrag]) {
        isDragging = YES;
        [viewDelegate viewBeganDragging];
    }
    
    if ([viewDelegate viewShouldTap]) {
        [viewDelegate viewDidTap];
    }
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (isDragging) {
        [viewDelegate viewEndedDragging];
    }
}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (isDragging) {
        [viewDelegate viewEndedDragging];
    }
}

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
    if (isDragging) {
        UITouch *touch = [touches anyObject];   
        CGPoint location = [touch locationInView:self.superview];
        
        self.center = location;
    }
}

@end
